package com.yindong.tabbar.utils;

import com.yindong.tabbar.model.TabBarInfoModel;
import ohos.aafwk.ability.AbilitySlice;
import ohos.app.AbilityContext;
import ohos.app.Context;
import org.apache.commons.jexl2.JexlContext;

import java.util.List;

/**
 * tabbar构建入口
 *
 * @author      殷冬
 * @date        2021-01-19
 * @version     1.0.0
 */
public class TabBarBuild {

    /**
     * tabBar配置信息json字符串
     */
    private String tabBarInfo;

    /**
     * tabBar配置信息
     */
    private List<TabBarInfoModel> tabBarInfoModels;

    public TabBarBuild(Builder builder){
        this.tabBarInfo = builder.tabBarInfo;
        this.tabBarInfoModels = builder.tabBarInfoModels;
    }

    public static class Builder{

        /**
         * tabBar配置信息json字符串
         */
        private String tabBarInfo;

        /**
         * tabBar配置信息
         */
        private List<TabBarInfoModel> tabBarInfoModels;

        /**
         * 获取配置文件信息
         *
         * @param abilityContext        上下文
         * @param profileTabBarId       资源文件id
         * @return
         */
        public Builder getTabBarConfig(AbilityContext abilityContext, Integer profileTabBarId){
            String tabBarInfo = TabBarConfigUtil.getTabBarConfig(abilityContext, profileTabBarId);
            this.tabBarInfo = tabBarInfo;
            return this;
        }

        /**
         * 解析tabbar配置信息，将配置信息中的ResourceTable.*值，转译成java代码并执行
         *
         * @param jexlContext       jexl字符串转译代码对象
         * @return
         */
        public Builder parseTabBarInfo(JexlContext jexlContext){
            List<TabBarInfoModel> tabBarInfoModels = TabBarConfigUtil.parseTabBarInfo(tabBarInfo, jexlContext);
            this.tabBarInfoModels = tabBarInfoModels;
            return this;
        }

        /**
         * 权限申请方法
         *
         * @param context           上下文
         * @param permissions       需要申请的权限列表
         */
        public Builder permission(AbilityContext context, String ... permissions){
            // 存储未授权的权限信息
            String[] unauthorizedInfo = new String[permissions.length];
            for(Integer i = 0; i<permissions.length; i++){
                // 判断是否已授权
                if(context.canRequestPermission(permissions[i])){
                    unauthorizedInfo[i] = permissions[i];
                }
            }
            // 非空进行授权
            if(! EmptyUtil.isEmpty(unauthorizedInfo)){
                // 在主线程中执行权限申请操作，否则看不到授权窗口
                context.getUITaskDispatcher().asyncDispatch(() -> {
                    context.requestPermissionsFromUser(unauthorizedInfo, 0);
                });
            }
            return this;
        }

        /**
         * 初始化Tabbar
         *
         * @param abilitySlice              abilitySlice对象
         * @param context                   上下文
         */
        public Builder initTabBar(AbilitySlice abilitySlice, Context context){
            TabBarInitUtil.initialization(abilitySlice, context, tabBarInfoModels);
            return this;
        }

        public void build(){
            new TabBarBuild(this);
        }
    }

}
